package org.game.gobang.client.support;

import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import org.game.component.AlertBox;
import org.game.gobang.client.constant.ClientConstant;
import org.game.result.RestResult;
import org.game.result.RestResultBuilder;
import org.game.result.ResultCode;
import org.game.util.JsonUtil;

public class CreateRoomBox extends AlertBox {
    public CreateRoomBox(String title) {
        super(title);
        display();
    }

    @Override
    protected Scene slot() {
        GridPane gridPane = new GridPane();

        gridPane.add(new Label("房间名"), 0, 0);
        TextField textField = new TextField();
        gridPane.add(textField, 1, 0);
        Button createButton = new Button("创建");
        createButton.setOnAction((action) -> {
            RestResult restResult = new RestResultBuilder().success(ResultCode.CREATE_ROOM);
            restResult.setData((Object) textField.getText());
            ClientConstant.ctx.writeAndFlush(JsonUtil.toJson(restResult));
            ClientConstant.refreshMap();

            close();
        });
        gridPane.add(createButton, 0, 1);

        Scene scene = new Scene(gridPane);
        return scene;
    }
}
